package edu.lsnu.equipment_management_system.pojo.domain;

import edu.lsnu.equipment_management_system.util.DateUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author mzy
 * @Description
 * @date 2018/11/26
 */
@Component
public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        if (source.length() == 0) {
            return null;
        }
        // yyyy-MM-dd 10
        // yyyy-MM-dd HH:mm:ss 19
        if(source.length() == 10) {
            return DateUtil.parseDate(source, "yyyy-MM-dd");
        } else if(source.length() == 19) {
            return DateUtil.parseDate(source, "yyyy-MM-dd HH:mm:ss");
        }

        return null;
    }

}